<template>
    <div class="table">
        <div class="container" style="padding: 10px !important;">
            <div style="">
                <el-tabs v-model="tabIndexActive" type="card" @tab-click="handleClick">
<!--                    <el-tab-pane label="控制参数" name="first"></el-tab-pane>-->
                    <el-tab-pane label="设备信息" name="second"></el-tab-pane>
                    <el-tab-pane label="策略信息" name="third"></el-tab-pane>
                </el-tabs>
            </div>
<!--控制参数-->
            <div v-if="tabIndexActive == 'first'">
                <el-table
                        :data="tableData"
                        border  stripe
                        style="width: 100%">
                    <el-table-column prop="date" align="center" label="控制名称"></el-table-column>
                    <el-table-column prop="name" align="center" label="控制结果"></el-table-column>
                    <el-table-column prop="address" align="center" label="下发时间"></el-table-column>
                    <el-table-column prop="address" align="center" label="完成时间"></el-table-column>
                    <el-table-column prop="address" align="center" label="备注"></el-table-column>
                </el-table>
            </div>
<!--      设备信息    -->
            <div v-if="tabIndexActive == 'second'">
                <el-table
                        :data="tableData"
                        border  stripe
                        height="300"
                        style="width: 100%">
                    <el-table-column prop="deviceCode" align="center" label="设备编号"></el-table-column>
                    <el-table-column prop="deviceName" align="center" label="设备名称"></el-table-column>
                    <el-table-column prop="concentratorName" align="center" label="所属协议"></el-table-column>
                    <el-table-column prop="deviceGroupName" align="center" label="所属分组"></el-table-column>
                    <el-table-column prop="concentratorName" align="center" label="所属集中器"></el-table-column>
                    <el-table-column prop="deviceUsingState" align="center" label="设备状态"></el-table-column>
                </el-table>
                <div class="pagination">
                    <el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next" :total="page_total" :page-size="page_size">
                    </el-pagination>
                </div>
            </div>
<!--            策略信息-->
            <div v-if="tabIndexActive == 'third'">
                <el-table
                        :data="tableList"
                        border  stripe
                        height="300"
                        style="width: 100%">
<!--                    <el-table-column type="selection" width="55"></el-table-column>-->
                    <el-table-column prop="strategyName" align="center" label="策略名称"></el-table-column>
                    <el-table-column prop="strategyStartTime" :formatter="formatDateAll" align="center" label="执行日期"></el-table-column>
                    <!--                <el-table-column prop="time" align="center" label="执行时间"></el-table-column>-->
                    <!--                <el-table-column prop="fun" align="center" label="执行动作"></el-table-column>-->
                    <!--                <el-table-column prop="control" align="center" label="光温控制"></el-table-column>-->
                    <el-table-column prop="strategyAction" :formatter="formatDes" align="center" label="策略描述"></el-table-column>
                </el-table>
            </div>

        </div>
    </div>

</template>

<script>
    import {getCategory,getTimerStrategy,dynamicSerachDeviceInfo,getStrategyForGroup} from '../../../api/index';
    export default {
        name: "equipmentGroupChild",
        props:['parentData'],
        data() {
            return {
                tableData: [],
                tabIndexActive:'second',
                tableList:[],
                actionList:[],
                checkWeek:['星期一','星期二','星期三','星期四','星期五','星期六','星期天'],
                cur_page:1,
                page_size:10,
                page_total:1,
                cur_id:''
            }
        },
        created(){
            this.getAction();
            this.getDataList();
            this.getGroupDeviceList();
            console.log(this.parentData)
        },
        watch:{
            parentData: function(val){
                // console.log(this);
                this.cur_id = val;
                this.cur_page = 1;
               this.getGroupDeviceList(val)
                this.getGroupStategList(val)
            }
        },
        methods:{
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getGroupDeviceList(this.cur_id)
            },
            handleClick(tab, event) {
                console.log(tab.name);
            },
            //获取组设备列表
            getGroupDeviceList(val){
                dynamicSerachDeviceInfo({
                    pageNum:this.cur_page,
                    pageSize:this.page_size,
                    groupId:val
                }).then((res)=>{
                    console.log(res)
                    this.tableData = res.data.list;
                    this.page_total = res.data.total;
                })
            },
            //获取策略列表
            getDataList(){
                getTimerStrategy({}).then((res)=>{
                    console.log(res);

                })
            },
            //获取设备动作列表
            getAction(){
                getCategory({
                    "dicName":"设备动作"
                }).then((res) =>{
                    console.log(res)
                    this.actionList = res.expandData
                })
            },
            getGroupStategList(){
                getStrategyForGroup({groupId:this.cur_id}).then((res)=>{
                    if(res.data){
                        this.tableList = [res.data];
                    }else{
                        this.tableList = [];
                    }
                    this.$emit("ListenChild",this.tableList)
                })
            },
            //格式化返回的有效时间
            formatDateAll(row,cellVal){
                return row.strategyStartTime + '-' + row.strategyEndTime;
            },
            //格式化策略描述信息
            formatDes(row, column, cellValue){
                var str = ''
                if(!row.strategyAction||row.strategyAction.length<=0){
                    return ;
                }
                for(var i=0;i<row.strategyAction.length;i++){
                    if(row.strategyAction[i].jobType == 1){
                        str += this.checkWeek[row.strategyAction[i].weekDay - 1] + ' '+row.strategyAction[i].jobTime +' '+ this.arrFind(row.strategyAction[i].jobAction) + '；'  ;
                    }else if(row.strategyAction[i].jobType == 2){
                        str +=  row.strategyAction[i].jobDate + ' '+ row.strategyAction[i].jobTime +' '+ this.arrFind(row.strategyAction[i].jobAction) + '；'   ;
                    }else{
                        str += '每天' + ' '+ row.strategyAction[i].jobTime +' '+ this.arrFind(row.strategyAction[i].jobAction) + '；'   ;
                    }
                }
                return str;
            },
            //数组查找
            arrFind(val){
                var curr =  this.actionList.find((currentValue)=>{
                    return  currentValue.value == val;
                })
                return curr?curr.name:''
            },
        }
    }
</script>

<style scoped>

</style>
